package com.shop2cn.iapi.prodstockmgr.testcase;

import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.prodstockmgr.parameter.GetStockOutInventoryListBean;
import com.shop2cn.iapi.prodstockmgr.service.GetStockOutInventoryListCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.YMTDateUtil;
/**
*出库单列表Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetStockOutInventoryList{
	private static GetStockOutInventoryListBean getstockoutinventorylistBean;
	private static GetStockOutInventoryListCall getstockoutinventorylistCall;
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	private static int csId = AgentUsers.csId;
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	private static int csId1 = AgentUsers.csId1;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("出库单列表Test");
		try {
			GodfreightmgrService.deleteBySellerId(rootUserId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		getstockoutinventorylistBean=new GetStockOutInventoryListBean();
		getstockoutinventorylistCall=new GetStockOutInventoryListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getstockoutinventorylistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_001() {
		Logger.start(true,"查新新增的出库单-默认type=0全部，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			String stockOut = Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			
			getstockoutinventorylistBean.setType(0);
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
			JsonArray asJsonArray = getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
			asJsonArray.forEach(b->{
				JsonObject inventory = b.getAsJsonObject();
				String inventoryNo = inventory.get("inventoryNo").getAsString();
				if(inventoryNo.equals(stockOut)){
					Logger.verifyEquals(-10, inventory.get("totalNum").getAsInt()*-1, "判断入库数量");
					Logger.verifyEquals(0, inventory.get("totalCost").getAsInt(), "判断总成本价");
				}
			});
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_0011() {
		Logger.start(true,"查询发货出库，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long stockOut = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			System.out.println(stockOut);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			//修改 wuwen 20200212
			//getstockoutinventorylistBean.setType(0);
			getstockoutinventorylistBean.setType(3);//0-所有，1-手动出库，2-发货出库 3调整库存 
			
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
			JsonArray asJsonArray = getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
			asJsonArray.forEach(b->{
				JsonObject inventory = b.getAsJsonObject();
				String inventoryNo = inventory.get("inventoryNo").getAsString();
				if(inventoryNo.equals(stockOut)){
					Logger.verifyEquals(-10, inventory.get("totalNum").getAsInt(), "判断入库数量");
					Logger.verifyEquals(0, inventory.get("totalCost").getAsInt(), "判断总成本价");
				}
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_0012() {
		Logger.start(true,"查询发货出库，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			
			getstockoutinventorylistBean.setType(0);
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_0013() {
		Logger.start(true,"查询手动出库+发货出库，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			
			getstockoutinventorylistBean.setType(0);
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_002() {
		Logger.start(true,"时间范围不正确，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			
			
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(false, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_003() {
		Logger.start(true,"商户id为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			
			
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();		
			//修改code wuwen 20200205
			Logger.verifyEquals("400", getstockoutinventorylistCall.getString("code"), "验证code");
			/*Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(false, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");*/
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_004() {
		Logger.start(true,"查新别人的的出库单，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			getstockoutinventorylistBean.setSellerId(vipRootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			
			
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(false, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_005() {
		Logger.start(true,"代理商查询渠道商的出库单，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			getstockoutinventorylistBean.setSellerId(agentUserId1);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			
			
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(false, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetStockOutInventoryList_006() {
		Logger.start(true,"商户没有添加出库单");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_007() {
		Logger.start(true,"pageIndex为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_008() {
		Logger.start(true,"查新新增的出库单-type=1手动入库，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//0手动入库，1调整库存
			String stockOut = Shop2cnStockCallService.stockOut(rootUserId, productId, 10,0);
			//0-所有，1-手动出库，2-发货出库 3调整库存
			getstockoutinventorylistBean.setType(1);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setType(0);
			
			getstockoutinventorylistBean.setType(0);
			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
			JsonArray asJsonArray = getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
			asJsonArray.forEach(b->{
				JsonObject inventory = b.getAsJsonObject();
				String inventoryNo = inventory.get("inventoryNo").getAsString();
				if(inventoryNo.equals(stockOut)){
					Logger.verifyEquals(-10, inventory.get("totalNum").getAsInt()*-1, "判断入库数量");
					Logger.verifyEquals(0, inventory.get("totalCost").getAsInt(), "判断总成本价");
				}
			});
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetStockOutInventoryList_009() {
		Logger.start(true,"查新新增的出库单-type=3调整入库，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//0手动入库，1调整库存
			String stockOut = Shop2cnStockCallService.stockOut(rootUserId, productId, 10,1);
			//0-所有，1-手动出库，2-发货出库 3调整库存
			getstockoutinventorylistBean.setType(3);
			getstockoutinventorylistBean.setSellerId(rootUserId);
			getstockoutinventorylistBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, "yyyy-MM-dd HH:mm:ss"));
			getstockoutinventorylistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy-MM-dd HH:mm:ss"));

			getstockoutinventorylistCall.setData(getstockoutinventorylistBean);
			getstockoutinventorylistCall.callService();			
			Logger.verifyEquals("200", getstockoutinventorylistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").get("totalCount").getAsInt()>0, "验证查询到的条数");
			
			JsonArray asJsonArray = getstockoutinventorylistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("inventoryList");
			asJsonArray.forEach(b->{
				JsonObject inventory = b.getAsJsonObject();
				String inventoryNo = inventory.get("inventoryNo").getAsString();
				if(inventoryNo.equals(stockOut)){
					Logger.verifyEquals(-10, inventory.get("totalNum").getAsInt()*-1, "判断入库数量");
					Logger.verifyEquals(0, inventory.get("totalCost").getAsInt(), "判断总成本价");
				}
			});
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	

	
	
}